package com.farm.area.service.impl;

import com.farm.area.dao.FarmLandMapper;
import com.farm.area.pojo.FarmLand;
import com.farm.area.service.FarmLandService;
import com.farm.entity.PageResult;
import com.farm.shop.feign.ShopFeign;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author azhe
 * @create 2020/10/12
 */
@Service
public class FarmLandServiceImpl implements FarmLandService {

    @Autowired
    private FarmLandMapper farmLandMapper;

    @Autowired
    private ShopFeign shopFeign;
    /**
     * 查询所有农田的详细信息
     * @return
     */
    @Override
    public List<Map<String,Object>> findAll() {
        return farmLandMapper.findAll();
    }

    /**
     * 安装设备
     * @param farmlandId
     * @param cdid
     * @param position
     * @return
     */
    @Override
    public int installDevice(Integer farmlandId, Integer cdid, String position) {
        Map map = new HashMap();
        //农田编号
        map.put("farmlandId",farmlandId);
        //用户的设备id
        map.put("cdid",cdid);
        //安装位置
        map.put("position",position);
        //运行状态 0未运行 1正在运行中
        map.put("status","0");
        //根据用户的设备id更改安装状态
        shopFeign.updateByCustDeviceId(cdid,position);

        int count = farmLandMapper.installDevice(map);
        return count;
    }

    /**
     * 种植农作物
     * @param farmlandId
     * @param cropsId
     * @return
     */
    @Override
    public int growCrops(Integer farmlandId, Integer cropsId) {
        Map<String, Object> map = new HashMap<>();
        //农田id
        map.put("farmlandId",farmlandId);
        //农作物id
        map.put("cropsId",cropsId);
        //开始时间
        map.put("startTime",new Date());
        //结束时间，像后推移30天
        Date date = dateRoll(new Date());
        map.put("endTime",date);
        //农作物生长阶段(生长阶段 0:幼苗期，1：成长期，2：成熟期)
        map.put("growStage",0);
        int count = farmLandMapper.growCrops(map);

        return count;
    }

    public Date dateRoll(Date date){
        //结束时间，像后推移30天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,30);
        date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        simpleDateFormat.format(date);
        return date;
    }

    /**
     * 根据大田id进行修改
     * @param farmLand
     * @return
     */
    @Override
    public int updateById(FarmLand farmLand) {
        int i = farmLandMapper.updateByPrimaryKeySelective(farmLand);
        return i;
    }

    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult findPage(Map<String, Object> searchMap, int page, int size) {
        if(page == 0){
            page = 1;
        }
        if(size == 0){
            size = 10;
        }
        //分页
        PageHelper.startPage(page,size);
        //条件查询
        Example example = new Example(FarmLand.class);
        Example.Criteria criteria = example.createCriteria();

        if(searchMap != null){
            if(searchMap.get("farmId") != null && !"".equals(searchMap.get("farmId"))){
                criteria.andEqualTo("farmId",searchMap.get("farmId"));
            }
        }

        Page<FarmLand> farmLandPage = (Page<FarmLand>) farmLandMapper.selectByExample(example);

        return new PageResult(farmLandPage.getTotal(),farmLandPage.getResult());
    }

    /**
     * 添加农田，并选择农场片区
     * @param farmLand
     * @param farmId
     * @return
     */
    @Override
    public boolean add(FarmLand farmLand, Integer farmId) {
        farmLand.setFarmId(farmId);
        //新增的大田添加状态为可用(0可用，1不可用)
        farmLand.setStatus("0");
        int i = farmLandMapper.insertSelective(farmLand);
        if(i <= 0){
           return false;
        }
        return true;
    }
}
